package com.whmskj.xjlhbc.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.whmskj.xjlhbc.domain.*;
import com.whmskj.xjlhbc.dto.JzkDto;
import com.whmskj.xjlhbc.mapper.MesdbRealMapper;
import com.whmskj.xjlhbc.mapper.MesktMapper;
import com.whmskj.xjlhbc.mapper.TvLargeScreenMapper;
import com.whmskj.xjlhbc.mobile.GetStartDate;
import com.whmskj.xjlhbc.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class ITvLargeScreenServiceImpl implements ITvLargeScreenService {

    @Autowired
    private IStatisticsService statisticsService;

    @Autowired
    private MesktMapper mesktMapper;

    @Autowired
    private TvLargeScreenMapper tvLargeScreenMapper;

    @Autowired
    private IRundataInfoService rundataInfoService;

    @Autowired
    private MesdbRealMapper mesdbRealMapper;

    @Autowired
    private IStatisticsDayZjService statisticsDayZjService;

    @Autowired
    private IStatisticsDayJsService statisticsDayJsService;

    @Override
    public Object get() {
        int hour = DateUtil.date().hour(true);
        String today = DateUtil.today();
        int year = DateUtil.date().year();
        int month = GetStartDate.getMonthValue();
        if(hour<=9){
            today = DateUtil.yesterday().toDateStr();
        }
        LambdaQueryWrapper<Statistics> statisticsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        statisticsLambdaQueryWrapper.eq(Statistics::getDateymd, today);
        List<Statistics> statisticsList = statisticsService.list(statisticsLambdaQueryWrapper);
        List<Statistics> jiaList = statisticsList.stream().filter(f -> f.getClassname().equals("甲")).collect(Collectors.toList());
        List<Statistics> yiList = statisticsList.stream().filter(f -> f.getClassname().equals("乙")).collect(Collectors.toList());
        double jiaEfficiency = jiaList.stream().mapToDouble(m -> NumberUtil.parseDouble(m.getEfficiency(), 0.0)).average().orElse(0.0);
        double yiEfficiency = yiList.stream().mapToDouble(m -> NumberUtil.parseDouble(m.getEfficiency(), 0.0)).average().orElse(0.0);
        double jiaZb = jiaList.stream().mapToDouble(m -> NumberUtil.parseDouble(m.getClothlength_zb(), 0.0)).sum();
        double yiZb = yiList.stream().mapToDouble(m -> NumberUtil.parseDouble(m.getClothlength_zb(), 0.0)).sum();
        Map<String, Object> resultMap = new HashMap<>();
        Map<String, Object> clMap = new HashMap<>();//实时班组产量
        Map<String, Object> bzMap = new HashMap<>();
        bzMap.put("xl", String.format("%.2f",jiaEfficiency));
        bzMap.put("cl", jiaZb);
        clMap.put("jia",bzMap);
        bzMap = new HashMap<>();
        bzMap.put("xl", String.format("%.2f",yiEfficiency));
        bzMap.put("cl", yiZb);
        clMap.put("yi",bzMap);
        LambdaQueryWrapper<Meskt> mesktLambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Meskt> mesktList = mesktMapper.selectList(mesktLambdaQueryWrapper);

        String classStartDate = GetStartDate.getClassStartDate();
        List<Map<String, Object>> varietiesData = tvLargeScreenMapper.selectVarietiesData(classStartDate,today,month,year);
        List<Map<String, Object>> employeeData = tvLargeScreenMapper.selectEmployeeData(classStartDate,today,month,year);

        LambdaQueryWrapper<MesdbReal> mesdbRealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        mesdbRealLambdaQueryWrapper.in(BaseEntity::getMacname, "db1", "db2").eq(MesdbBaseEntity::getDbname,"总表");
        List<MesdbReal> mesdbRealList = mesdbRealMapper.selectList(mesdbRealLambdaQueryWrapper);
        double jrdh = mesdbRealList.stream().mapToDouble(m -> NumberUtil.parseDouble(m.getDay(), 0.0)).sum();

        resultMap.put("cl", clMap); //实时班组产量
        resultMap.put("kt", mesktList);//能环数据,空调
        resultMap.put("varieties", varietiesData);//品种生产数据
        resultMap.put("rycl", employeeData);//品种生产数据
        resultMap.put("jrdh", String.format("%.2f", jrdh));//今日电耗
        return resultMap;
    }

    @Override
    public Object get1() {
        LambdaQueryWrapper<RundataInfo> rundataInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        return rundataInfoService.list(rundataInfoLambdaQueryWrapper);
    }

    @Override
    public Object get2() {
        int hour = DateUtil.date().hour(true);
        String today = DateUtil.today();
        int year = DateUtil.date().year();
        int month = GetStartDate.getMonthValue();
        if(hour<=11){
            today = DateUtil.yesterday().toDateStr();
        }
        DateTime dateTime = DateUtil.offsetDay(new Date(), -30);
        String dateymd = dateTime.toDateStr();
        List<Map<String, Object>> list = tvLargeScreenMapper.selectClassCl(dateymd); //ban组生产对比
        List<Map<String, Object>> list1 = tvLargeScreenMapper.selectGw(today,month,year);//岗位绩效对比
        Map<String, Object> map = new HashMap<>();
        map.put("cl", list);
        map.put("gw", list1);
        return map;
    }

    @Override
    public Object get3() {
        Map<String, Object> resultMap = new HashMap<>();
        Map<String, Object> jtMap = new HashMap<>();
        jtMap.put("yx", 41);
        jtMap.put("lt", 0);
        jtMap.put("dj", 0);
        jtMap.put("wb", 0);
        jtMap.put("lx", 0);
        Map<String, Object> clMap = new HashMap<>();
        String today = DateUtil.today();
        LambdaQueryWrapper<StatisticsDayZj> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StatisticsDayZj::getDateymd, today);
        List<StatisticsDayZj> statisticsDayZjList = statisticsDayZjService.list(wrapper);
        double cl = statisticsDayZjList.stream().mapToDouble(m -> NumberUtil.parseDouble(m.getClothlength(), 0.0)).sum();
        double zs = statisticsDayZjList.stream().mapToLong(m -> NumberUtil.parseLong(m.getZs(), 0L)).sum();
        LambdaQueryWrapper<StatisticsDayJs> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(StatisticsDayJs::getDateymd, today);
        List<StatisticsDayJs> statisticsDayJsList = statisticsDayJsService.list(wrapper1);
        double cl1 = statisticsDayJsList.stream().mapToDouble(m -> NumberUtil.parseDouble(m.getClothlength(), 0.0)).sum();
        double zs1 = statisticsDayJsList.stream().mapToLong(m -> NumberUtil.parseLong(m.getZs(), 0L)).sum();
        Map<String, Object> clMap1 = new HashMap<>();
        clMap1.put("zs", zs);
        clMap1.put("cl", String.format("%.2f", cl / 10000.0));
        Map<String, Object> clMap2 = new HashMap<>();
        clMap2.put("zs", zs1);
        clMap2.put("cl", String.format("%.2f", cl1 / 10000.0));
        clMap.put("zj", clMap1);
        clMap.put("js", clMap2);

        Map<String, Object> nhMap = tvLargeScreenMapper.selectDh();//准整车间电耗
        List<Map<String, Object>> ljMap = new ArrayList<>();//准整车间累计数据
        DateTime date = DateUtil.date();
        DateTime beginOfDay = DateUtil.beginOfDay(date);
        DateTime endOfDay = DateUtil.endOfDay(date);
        DateTime beginOfWeek = DateUtil.beginOfWeek(date);
        DateTime endOfWeek = DateUtil.endOfWeek(date);
        DateTime beginOfMonth = DateUtil.beginOfMonth(date);
        DateTime endOfMonth = DateUtil.endOfMonth(date);
        DateTime beginOfYear = DateUtil.beginOfYear(date);
        DateTime endOfYear = DateUtil.endOfYear(date);
        double zjclBr = tvLargeScreenMapper.selectZjcl(beginOfDay.toString(), endOfDay.toString());
        double zjclBz = tvLargeScreenMapper.selectZjcl(beginOfWeek.toString(), endOfWeek.toString());
        double zjclBy = tvLargeScreenMapper.selectZjcl(beginOfMonth.toString(), endOfMonth.toString());
        double zjclBn = tvLargeScreenMapper.selectZjcl(beginOfYear.toString(), endOfYear.toString());
        Map<String, Object> map = new HashMap<>();
        map.put("name", "整经产量m");
        map.put("r", zjclBr);
        map.put("z", zjclBz);
        map.put("y", zjclBy);
        map.put("n", zjclBn);
        ljMap.add(map);
        double zjdhBr = NumberUtil.parseDouble(String.valueOf(nhMap.get("zj")),0.0);//今日电耗
        Map<String, Object> dhMap = tvLargeScreenMapper.selectDhByDateRange(beginOfWeek.toString(), endOfWeek.toString());
        double zjdhBz = NumberUtil.parseDouble(String.valueOf(dhMap.get("zj")),0.0);
        dhMap = tvLargeScreenMapper.selectDhByDateRange(beginOfMonth.toString(), endOfMonth.toString());
        double zjdhBy = NumberUtil.parseDouble(String.valueOf(dhMap.get("zj")),0.0);
        dhMap = tvLargeScreenMapper.selectDhByDateRange(beginOfYear.toString(), endOfYear.toString());
        double zjdhBn = NumberUtil.parseDouble(String.valueOf(dhMap.get("zj")), 0.0);
        map = new HashMap<>();
        map.put("name", "整经电耗kwh");
        map.put("r", zjdhBr);
        map.put("z", zjdhBz);
        map.put("y", zjdhBy);
        map.put("n", zjdhBn);
        ljMap.add(map);
        map = new HashMap<>();
        map.put("name", "整经米布电耗");
        map.put("r", 0);
        map.put("z", 0);
        map.put("y", 0);
        map.put("n", 0);
        ljMap.add(map);

        double jsclBr = tvLargeScreenMapper.selectJscl(beginOfDay.toString(), endOfDay.toString());
        double jsclBz = tvLargeScreenMapper.selectJscl(beginOfWeek.toString(), endOfWeek.toString());
        double jsclBy = tvLargeScreenMapper.selectJscl(beginOfMonth.toString(), endOfMonth.toString());
        double jsclBn = tvLargeScreenMapper.selectJscl(beginOfYear.toString(), endOfYear.toString());
        map = new HashMap<>();
        map.put("name", "浆纱产量m");
        map.put("r", String.format("%.2f",jsclBr));
        map.put("z", String.format("%.2f",jsclBz));
        map.put("y", String.format("%.2f",jsclBy));
        map.put("n", String.format("%.2f",jsclBn));
        ljMap.add(map);
        double jsdhBr = NumberUtil.parseDouble(String.valueOf(nhMap.get("js")),0.0);//今日电耗
        Map<String, Object> jsdhMap = tvLargeScreenMapper.selectDhByDateRange(beginOfWeek.toString(), endOfWeek.toString());
        double jsdhBz = NumberUtil.parseDouble(String.valueOf(jsdhMap.get("js")),0.0);
        jsdhMap = tvLargeScreenMapper.selectDhByDateRange(beginOfMonth.toString(), endOfMonth.toString());
        double jsdhBy = NumberUtil.parseDouble(String.valueOf(jsdhMap.get("js")),0.0);
        jsdhMap = tvLargeScreenMapper.selectDhByDateRange(beginOfYear.toString(), endOfYear.toString());
        double jsdhBn = NumberUtil.parseDouble(String.valueOf(jsdhMap.get("js")), 0.0);
        map = new HashMap<>();
        map.put("name", "浆纱电耗kwh");
        map.put("r", jsdhBr);
        map.put("z", jsdhBz);
        map.put("y", jsdhBy);
        map.put("n", jsdhBn);
        ljMap.add(map);
        map = new HashMap<>();
        map.put("name", "浆纱米布电耗");
        map.put("r", 0);
        map.put("z", 0);
        map.put("y", 0);
        map.put("n", 0);
        ljMap.add(map);

        double gldhBr = NumberUtil.parseDouble(String.valueOf(nhMap.get("gl")),0.0);//今日电耗
        Map<String, Object> gldhMap = tvLargeScreenMapper.selectDhByDateRange(beginOfWeek.toString(), endOfWeek.toString());
        double gldhBz = NumberUtil.parseDouble(String.valueOf(gldhMap.get("gl")),0.0);
        gldhMap = tvLargeScreenMapper.selectDhByDateRange(beginOfMonth.toString(), endOfMonth.toString());
        double gldhBy = NumberUtil.parseDouble(String.valueOf(gldhMap.get("gl")),0.0);
        gldhMap = tvLargeScreenMapper.selectDhByDateRange(beginOfYear.toString(), endOfYear.toString());
        double gldhBn = NumberUtil.parseDouble(String.valueOf(gldhMap.get("gl")), 0.0);
        map = new HashMap<>();
        map.put("name", "锅炉电耗kwh");
        map.put("r", gldhBr);
        map.put("z", gldhBz);
        map.put("y", gldhBy);
        map.put("n", gldhBn);
        ljMap.add(map);

        double zdhdhBr = NumberUtil.parseDouble(String.valueOf(nhMap.get("zdh")),0.0);//今日电耗
        Map<String, Object> zdhdhMap = tvLargeScreenMapper.selectDhByDateRange(beginOfWeek.toString(), endOfWeek.toString());
        double zdhdhBz = NumberUtil.parseDouble(String.valueOf(zdhdhMap.get("zdh")),0.0);
        zdhdhMap = tvLargeScreenMapper.selectDhByDateRange(beginOfMonth.toString(), endOfMonth.toString());
        double zdhdhBy = NumberUtil.parseDouble(String.valueOf(zdhdhMap.get("zdh")),0.0);
        zdhdhMap = tvLargeScreenMapper.selectDhByDateRange(beginOfYear.toString(), endOfYear.toString());
        double zdhdhBn = NumberUtil.parseDouble(String.valueOf(zdhdhMap.get("zdh")), 0.0);
        map = new HashMap<>();
        map.put("name", "车间总电耗kwh");
        map.put("r", zdhdhBr);
        map.put("z", zdhdhBz);
        map.put("y", zdhdhBy);
        map.put("n", zdhdhBn);
        ljMap.add(map);
        List<Map<String,Object>> jzkTj = tvLargeScreenMapper.selectJzkTj();
        List<JzkDto.Bh> bhList = jzkTj.stream().map(item->{
            JzkDto.Bh bh = new JzkDto.Bh();
            bh.setBh(item.get("bubbleNumber").toString());
            bh.setK1(NumberUtil.parseLong(String.valueOf(item.get("room01190")), 0L));
            bh.setK2(NumberUtil.parseLong(String.valueOf(item.get("room02230")), 0L));
            bh.setK3(NumberUtil.parseLong(String.valueOf(item.get("ground190")), 0L));
            bh.setK4(NumberUtil.parseLong(String.valueOf(item.get("ground230")), 0L));
            bh.setZj(bh.getK1()+bh.getK2()+bh.getK3()+bh.getK4());
            return bh;
        }).collect(Collectors.toList());
        resultMap.put("jt", jtMap);
        resultMap.put("sscl", clMap);
        resultMap.put("nh", nhMap);
        resultMap.put("jzk", bhList);
        resultMap.put("ljsj", ljMap);
        return resultMap;
    }

    @Override
    public Object get4() {
        return tvLargeScreenMapper.selectScrollText();
    }
}
